package com.colin.app.popupwindow;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;

import com.colin.app.R;
import com.colin.app.activity.method.blur.JavaBlurProcess;

/**
 * Created by Colin on 2017/6/4.
 */

public class BlurPopupWindow extends PopupWindow {
    private Context context;
    private View view;
    private ImageView image_pop_gb;


    public BlurPopupWindow(Context context, Bitmap bitmap) {
        super(context);
        this.context = context;
        initView(bitmap);
    }


    private void initView(Bitmap bitmap) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(R.layout.pop_blur_view, null);
        this.image_pop_gb = (ImageView) view.findViewById(R.id.image_pop_gb);
        this.view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
//        image_pop_gb.setImageBitmap(ImageHelp.fastBlur(bitmap, 0.8F, 20F));
        blur(image_pop_gb, bitmap, 9, 20);
        if (bitmap != null) {
            bitmap.recycle();
        }
        // 设置允许在外点击消失
        this.setOutsideTouchable(false);
        // 设置SelectPicPopupWindow的View
        this.setContentView(view);
        // 设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(LinearLayout.LayoutParams.MATCH_PARENT);
        // 设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(LinearLayout.LayoutParams.MATCH_PARENT);
        // 设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
        // 设置SelectPicPopupWindow弹出窗体动画效果
        this.setAnimationStyle(android.R.style.Animation_InputMethod);
        // 设置背景色
        Resources resources = view.getResources();
        Drawable drawable = resources.getDrawable(android.R.color.white);
        this.setBackgroundDrawable(drawable);
        //允许弹出窗口超出屏幕范围
        this.setClippingEnabled(false);

    }

    Bitmap overlay;

    private void blur(ImageView view, Bitmap bkg, float blurRadius, int scaleRadius) {
        if (overlay != null) {
            overlay.recycle();
        }
        overlay = Bitmap.createScaledBitmap(bkg, bkg.getWidth() / scaleRadius, bkg.getHeight() / scaleRadius, false);
        overlay = new JavaBlurProcess().blur(overlay, blurRadius);//高斯模糊
        view.setImageBitmap(overlay);
    }


}
